<?php
namespace Info\Controller;

use Common\Controller\BehaviorController;
use Common\Model\InfoCategoryModel;
use Common\Model\InfoContentModel;
use Common\Model\UserModel;

class CommonController extends BehaviorController
{

    public function __construct()
    {
        parent::__construct();
        $this->getUserInfo();
    }

    /**
     * 获取列表页栏目分类
     */
    public function getListCategory($pid)
    {
        $categoryModel = new InfoCategoryModel();
        if (empty($pid)) {
            $category = $categoryModel->getCategory(1);
        } else {
            $category = $categoryModel->getCategory($pid);
        }
        return $category;
    }


    public function getUserInfo()
    {
        $userKey = C('COOK_LOGIN_KEY') . '_uid';
        $userValue = cookie($userKey);
        if ($userValue) {
            $userType = explode('_', $userValue);
            if ($userKey && isset($userKey[0])) {
                if ((int)$userKey[0] > 0) {
                    $uid = isset($userKey[1]) ? $userKey[1] : 0;
                }else{
                    $uid = $userKey[0];
                }
                if($uid){
                    $userModel = new UserModel();
                    $user = $userModel->getUserById($uid);
                    $this->assign('user',$user);
                }else{
                    cookie($userKey,null);
                }
            }
        } else {
            return false;
        }
    }

    public function getAllCategory($href = true)
    {
        $categoryModel = new InfoCategoryModel();
        $allCategory = $categoryModel->getAllCategory();
        if (empty($allCategory)) throw_exception('栏目异常,请稍后访问~ ');
        $category = array();
        $categoryName = array_column($allCategory, 'category', 'id');
        foreach ($allCategory as $k => $v) {
            if ($v['pid'] != 0) {
                if ($href) {
                    $category[$v['id']] = "【<a href ='" . UU('Info/list/index', array('pid' => $v['pid'])) . "' >" . $categoryName[$v['pid']] . "</a>/<a href ='" . UU('Info/list/index', array('pid' => $v['pid'], 'cid' => $v['id'])) . "' >" . $categoryName[$v['id']] . "</a>】";
                } else {
                    $category[$v['id']] = "【" . $categoryName[$v['pid']] . "/" . $categoryName[$v['id']] . "】";
                }
            }
        }
        return $category;
    }

    public function getCategoryByPid()
    {
        $categoryModel = new InfoCategoryModel();
        $category = $categoryModel->getCategoryGroPid();
        $cid = array_column($category, 'category', 'id');
        $pid = array_column($category, 'category', 'pid');
        $temp = array();
        foreach ($category as $k => $v) {
            $temp[$v['pid']][] = $v;
        }
        $data = array(
            'cid' => $cid,
            'pid' => $pid,
            'data' => $temp
        );
        return $data;
    }

    public function getInfoInCidArr($cidArr, $limit = null)
    {
        $infoModel = new InfoContentModel();
        return $infoModel->getInfoInCidArr($cidArr, $limit);
    }

    /**
     * 信息页 获取相关栏目分类
     */
    public function getInfoCategory($cid, $pid)
    {
        $categoryModel = new InfoCategoryModel();
        if (empty($pid)) {
            $category = $categoryModel->getCategoryById($cid);
        } else {
            $category = $categoryModel->getCategoryByPid($pid);
        }
        return $category;
    }

    /**
     * 信息页 获取相关信息
     */
    public function getInfoById($id)
    {
        $info = new InfoContentModel();
        $data = $info->find($id);
        return $data;
    }

    public function getInfoInId($idArr)
    {
        $info = new InfoContentModel();
        $data = $info->getInfoInId($idArr);
        return $data;
    }

    /**
     * 面包屑
     */
    public function getFatherCategory($cid)
    {
        $categoryModel = new InfoCategoryModel();
        $category = $categoryModel->getFatherCategory($cid);
        $father = $categoryModel->getCategoryById($category['pid']);
        $data = array(
            'current' => $category,
            'father' => $father,
        );
        return $data;
    }

    public function getInfosByPhone($phone)
    {
        $infoModel = new InfoContentModel();
        return $infoModel->getInfosByPhone($phone);
    }

    public function error404()
    {
        echo '404';
    }
}